<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
     <el-form-item label="产线" prop="shopId">
              <el-select
                v-model="queryParams.shopId"
                placeholder="请选择产线"
                style="width: 240px"
                filterable
                @change="productionLineChange"
              >
                <el-option
                  v-for="dict in productionLineList"
                  :key="dict.id"
                  :label="dict.label"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
             <el-form-item label="工位名称" prop="stationId">
              <el-select
                v-model="queryParams.stationId"
                placeholder="请选择工位名称"
                style="width: 240px"
                filterable
              >
                <el-option
                  v-for="dict in stationList"
                  :key="dict.shopId"
                  :label="dict.shopName"
                  :value="dict.shopId"
                ></el-option>
              </el-select>
            </el-form-item>
      <el-form-item label="物料名称" prop="materialId">
        <el-select
          filterable
          v-model="queryParams.materialId"
          placeholder="请选择物料名称"
          style="width: 240px"
        >
          <!-- <el-option
            v-for="dict in materialList"
            :key="dict.dataId"
            :label="dict.invName"
            :value="dict.dataId"
          ></el-option> -->
           <el-option
                  v-for="dict in materialList"
                  :key="dict.dataId"
                  :value="dict.dataId"
                  :label="dict.invName+'/'+dict.invCode"
                >
                {{dict.invName+'/'+dict.invCode}}
                </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="stationMaterialList"
      row-key="id"
      style="width: 100%"
    >
      <el-table-column
        label="主键id"
        align="center"
        prop="id"
        v-if="false"
      />
      <el-table-column label="工位名称" align="center" prop="stationName" />
      <el-table-column label="工位编码" align="center" prop="stationCode" />

      <el-table-column label="物料编码" align="center" prop="materialCode" />
      <el-table-column label="物料名称" align="center" prop="materialName" />
      <el-table-column label="产线编码" align="center" prop="shopCode" />
      <el-table-column label="产线名称" align="center" prop="shopName" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            style="margin-right: 9px"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改物料信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row :gutter="20">
          <el-col :lg="12">
            <el-form-item label="产线" prop="shopId">
              <el-select
                v-model="form.shopId"
                placeholder="请选择产线"
                style="width: 240px"
                filterable
                @change="productionLineChange"
              >
                <el-option
                  v-for="dict in productionLineList"
                  :key="dict.id"
                  :label="dict.label"
                  :value="dict.id"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工位名称" prop="stationId">
              <el-select
                v-model="form.stationId"
                placeholder="请选择工位名称"
                style="width: 240px"
                filterable
              >
                <el-option
                  v-for="dict in stationList"
                  :key="dict.shopId"
                  :label="dict.shopName"
                  :value="dict.shopId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料名称" prop="materialId">
              <el-select
                filterable
                v-model="form.materialId"
                placeholder="请选择物料名称"
                style="width: 240px"
              >
                <!-- <el-option
                  v-for="dict in materialList"
                  :key="dict.dataId"
                  :label="dict.invName"
                  :value="dict.dataId"
                ></el-option> -->
                <el-option
                  v-for="dict in materialList"
                  :key="dict.dataId"
                  :value="dict.dataId"
                 :label="dict.invName+'/'+dict.invCode"
                >
                {{dict.invName+'/'+dict.invCode}}
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMaterial,
  getMaterial,
  delMaterial,
  addMaterial,
  updateMaterial,
  exportMaterial,
} from "@/api/base/materialERP";
import { treeselect, getTreeFindId, listShop } from "@/api/base/shop";
import {
  listOperations,
  addOperations,
  updateOperations,
  delOperations,
  getStationMaterial,
} from "@/api/base/stationMaterial";
export default {
  name: "Material",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料信息表格数据
      materialList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 物料类型字典
      materialTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialId: undefined,
        stationId: undefined,
        shopId:undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        materialId: [
          { required: true, message: "物料不能为空", trigger: "change" },
        ],
        shopId: [
          { required: true, message: "产线不能为空", trigger: "change" },
        ],
        stationId: [
          { required: true, message: "工位不能为空", trigger: "change" },
        ],
      },
      //   产线
      productionLineList: [],
      //   工位物料
      stationMaterialList: [],
      //   工位
      stationList: [],
    };
  },
  created() {
    this.getList();
    this.getProductionLineList();
    this.getMaterialList();
    this.getDicts("mes_material_type").then((response) => {
      this.materialTypeOptions = response.data;
    });
  },
  methods: {
    // 产线选择
    productionLineChange(e) {
      listShop({ parentId: e }).then((res) => {
        this.stationList = res.rows;
      });
    },
    /** 查询产线 */
    getProductionLineList() {
      treeselect().then((res) => {
        this.productionLineList = res.data[0].children[0].children[0].children;
      });
    },
    // 查询物料
    getMaterialList() {
      listMaterial().then((response) => {
        this.materialList = response.rows;
      });
    },
    /** 查询物料信息列表 */
    getList() {
      this.loading = true;
      listOperations(this.queryParams).then((response) => {
        this.stationMaterialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增工位物料";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      getStationMaterial(row.id).then((res) => {
        listShop({ parentId: res.data?.shopId }).then((res1) => {
          this.stationList = res1.rows;
          this.form = res.data;
          this.open = true;
          this.title = "修改工位物料";
          this.loading = false;
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOperations(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addOperations(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm("是否确认删除该数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delOperations(row.id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
  },
};
</script>